#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/select.h>
#include "ADS_B.h"
#define rerecv "1"
#define minSDistance 100

using namespace std;

//void* test(void* cnt);


int main() {
    std::array<AircraftState*,1024> arr{};
    initarr(arr);
    int soc;
    struct sockaddr_in ser, cli;
    memset(&ser, 0, sizeof(ser));
    memset(&cli, 0, sizeof(cli));
    ser.sin_family = AF_INET;
    ser.sin_port = htons(7777);
    ser.sin_addr.s_addr = inet_addr("192.168.84.111");
    soc = socket(AF_INET, SOCK_STREAM, 0);
    if (soc == -1) {
        perror("create soc,error\n");
        exit(1);
    }
    if (bind(soc, (struct sockaddr *) &ser, sizeof(ser)) == -1) {
        perror("bind error");
        exit(1);
    };
    if (listen(soc, 10) == -1) {
        perror("listen error");
        exit(1);
    }
    fd_set readfd;
    FD_ZERO(&readfd);
    FD_SET(soc, &readfd);
    int max_fd = soc;
    timeval timeout;
    timeout.tv_sec = 1;//每1秒检测一次可根据实际需求更改
    timeout.tv_usec = 0;
    while (1) {
        fd_set tempfd = readfd;
        select(max_fd + 1, &tempfd, NULL, NULL, &timeout);
        if (FD_ISSET(soc, &tempfd)) {
            int cfd = accept(soc, NULL, NULL);
            FD_SET(cfd, &readfd);
            max_fd = max_fd > cfd ? max_fd : cfd;
            std::cout<<"1"<<std::endl;
            std::cout<<"in fdset"<<std::endl;
        }
        for (int i = 0; i <= max_fd; i++) {
            if (FD_ISSET(i, &tempfd) && (i != soc)) {
                // int* ac;
                char bust[50] ;
                memset(bust,0,sizeof (bust));
                int len1=recv(i, bust, 50,0);

                if(len1==-1){//接收数据失败
                    send(i,rerecv,(strlen(rerecv)+1),0);
                    perror("recv error");
                    exit(1);
                }else if(len1==0){//客户端已经断开连接
                    std::cout<<"已经退出"<<std::endl;
                    FD_CLR(i,&readfd);
                    delete arr[i];
                    arr[i]=nullptr;
                    close(i);
                } else {
                    char x[10] ;//x轴
                    memset(x,0,sizeof(x));
                    strcpy(x, bust);
                    printf("recv x=%s\n", bust);
                    char y[10] ;//y轴
                    memset(y,0,sizeof(y));
                    strcpy(y, bust+10);
                    printf("recv y=%s\n", bust+10);
                    char vx[10] ;//x轴速度
                    memset(vx,0,sizeof(vx));
                    strcpy(vx, bust+20);
                    printf("recv vx=%s\n",bust+20);
                    char vy[10];//y轴速度
                    memset(vy,0,sizeof(vy));
                    strcpy(vy,bust+30);
                    printf("recv vy=%s\n",vy);
                    char heading[10];//航向
                    memset(heading,0,sizeof(heading));
                    strcpy(heading,bust+40);
                    printf("recv heading=%s\n",heading);
                    arr[i]=new AircraftState(stod(x),stod(y), stod(vx),stod(vy),stod(heading));
                    bool icon=checkCollisionRisk(i,*arr[i], arr, minSDistance, 3);

                    if(icon == true){
                        std::cout<<"存在风险"<<std::endl;
                    }else{
                        char rec[3] = "ok";
                        send(i, rec, (strlen(rec) + 1), 0);
                    }
                }

            }
        }

    }
}



